package vip.xiaonuo.modular.reactor;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant.DeviceSyncStatus;
import vip.xiaonuo.modular.remoteapi.stairs.RemoteStairsConstant.RequestCmd;
import vip.xiaonuo.modular.sync.DeviceEvent;
import vip.xiaonuo.modular.ybuildingmemberrl.entity.YBuildingMemberRl;
import vip.xiaonuo.modular.ybuildingmemberrl.mapper.YBuildingMemberRlMapper;
import vip.xiaonuo.modular.ybuildingmemberrl.param.YBuildingMemberRlParam;
import vip.xiaonuo.modular.ydeviceevent.entity.YDeviceEvent;
import vip.xiaonuo.modular.ydeviceevent.mapper.YDeviceEventMapper;
import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static reactor.event.selector.Selectors.$;

@Service
public class StarisMemberUpdateCardListener implements Consumer<Event<Long>>, InitializingBean {

    @Resource
    private YBuildingMemberRlMapper buildingMemberRlMapper;

    @Autowired
    private YElevatorDeviceService elevatorDeviceService;

    @Resource
    private YDeviceEventMapper deviceEventEntityMapper;

    @Autowired
    private Reactor reactor;

    @Autowired
    private MemberUserService memberUserService;

    @Override
    public void accept(Event<Long> event) {
        List<YBuildingMemberRl> list = buildingMemberRlMapper.findList(new YBuildingMemberRlParam().setMemberId(event.getData()));
        MemberUser memberUser = memberUserService.getById(event.getData());
        if (StringUtils.isBlank(memberUser.getIcCard())) {
            memberUser.setReadCardStatus("");
            memberUserService.updateById(memberUser);
            // 卡号为null 删除梯控人员
            for (YBuildingMemberRl yBuildingMemberRl : list) {
                // 新增中 直接删除
                if (ObjectUtil.equal(yBuildingMemberRl.getDeviceSyncStatus(), DeviceSyncStatus.ADD)) {
                    buildingMemberRlMapper.deleteById(yBuildingMemberRl.getId());
                    continue;
                }
                // 其他的删除梯控任务
                yBuildingMemberRl.setDeviceSyncStatus(DeviceSyncStatus.DELETE);
                buildingMemberRlMapper.updateById(yBuildingMemberRl);
                YElevatorDevice device = elevatorDeviceService.getById(yBuildingMemberRl.getDeviceId());
                YDeviceEvent deviceEvent = new YDeviceEvent();
                deviceEvent.setDeviceNo(device.getDeviceNo());
                deviceEvent.setStatus(DeviceEvent.NEW);
                deviceEvent.setCreateDate(new Date());
                deviceEvent.setSysType(1);
                deviceEvent.setBody(yBuildingMemberRl.getMemberId()+":"+yBuildingMemberRl.getMemberId());
                deviceEvent.setType(RequestCmd.DELETE_PERSON);
                deviceEventEntityMapper.insert(deviceEvent);
            }
            return;
        }

        if (CollectionUtils.isNotEmpty(list)) {
            for (YBuildingMemberRl yBuildingMemberRl : list) {
                if (ObjectUtil.equal(yBuildingMemberRl.getDeviceSyncStatus(), DeviceSyncStatus.SYNC_SUCCESS)) {
                    // 修改卡号
                    yBuildingMemberRl.setDeviceSyncStatus(RemoteStairsConstant.DeviceSyncStatus.UPDATE);
                    buildingMemberRlMapper.updateById(yBuildingMemberRl);
                    // 插入人员修改任务表
                    YElevatorDevice device = elevatorDeviceService.getById(yBuildingMemberRl.getDeviceId());
                    YDeviceEvent deviceEvent = new YDeviceEvent();
                    deviceEvent.setDeviceNo(device.getDeviceNo());
                    deviceEvent.setStatus(DeviceEvent.NEW);
                    deviceEvent.setCreateDate(new Date());
                    deviceEvent.setSysType(1);
                    deviceEvent.setBody(yBuildingMemberRl.getId().toString());
                    deviceEvent.setType(RemoteStairsConstant.RequestCmd.UPDATE_PERSON);
                    deviceEventEntityMapper.insert(deviceEvent);
                }
                }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.MEMBER_UPDATE_CARD), this);
    }
}
